/**
 * Created by zy on 2016/6/14.
 * 风险评价 - 推送设置 - 系统设置 - 系统设置
 */


;(function ($, window, document, undefined) {

    /**
     * 获取设置数据 - start
     */
    var baseData = function () {
        var temp = '';
        DyyBase.ajax(sysApi.url_settings_threshold(), {type: 'RISK_EVALUATION'}, function (dataset) {
            temp = dataset.result;
        }, {async: false});
        return temp;
    }();
    /**
     * 获取设置数据 - end
     */

    //console.log(baseData);

    /**
     * 风险推送控件初始化 - start
     */
    // 风险等级
    var risk_grde = function () {
        var temp = $.trim($('#JQ_data_risk_grde').html());
        temp = eval('(' + temp + ')');
        //temp.shift();
        return DyyBase.getSelect(temp);
    }();

    // 高后果区
    var area_level = function () {
        var temp = $.trim($('#JQ_data_area_level').html());
        temp = eval('(' + temp + ')');
        //temp.shift();
        return DyyBase.getSelect(temp);
    }();

    // 角色列表
    var role_list = function () {
        var temp = $.trim($('#JQ_data_role_list').html());
        temp = eval('(' + temp + ')');
        return temp;
    }();

    // 角色对应
    var role_map = function () {
        var temp = $.trim($('#JQ_data_role_map').html());
        temp = eval('(' + temp + ')');
        return temp;
    }();

    // 风险等级 01
    var $risk_grde_01 = $('#JQ_risk_grde_01').select3({
        search: false,
        data: risk_grde,
        allowClear: false
    });
    // 风险等级 02
    var $risk_grde_02 = $('#JQ_risk_grde_02').select3({
        search: false,
        data: risk_grde,
        allowClear: false
    });
    // 风险等级 03
    var $risk_grde_03 = $('#JQ_risk_grde_03').select3({
        search: false,
        data: risk_grde,
        allowClear: false
    });
    // 高后果区 01
    var $area_level_01 = $('#JQ_area_level_01').select3({
        search: false,
        data: area_level,
        allowClear: false
    });
    // 高后果区 02
    var $area_level_02 = $('#JQ_area_level_02').select3({
        search: false,
        data: area_level,
        allowClear: false
    });
    // 高后果区 03
    var $area_level_03 = $('#JQ_area_level_03').select3({
        search: false,
        data: area_level,
        allowClear: false
    });
    /**
     * 风险推送控件初始化 - end
     */


    /**
     * 评价周期控件初始化 - start
     */
    // 管道
    var $pipeline = $('#JQ_pipeline').select3({
        allowClear: false,
        search: false
    });
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段',
        search: false,
        allowClear: false
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        search: false,
        title: '起点位置',
        server: true,
        allowClear: false,
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment
        }
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        search: false,
        title: '终点位置',
        server: true,
        allowClear: false,
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment,
            'beginSignId': $begin
        }
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v, t) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            if(t === undefined){
                DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                    var begin = DyyBase.getPos(dataset, 'begin');
                    var end = DyyBase.getPos(dataset, 'end');
                    $begin.asset(begin);
                    $end.asset(end);
                });
            }
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    // 初次评价时间
    $.datetimepicker.setLocale('zh');
    var $date = $('#JQ_date');
    $date.datetimepicker({
        timepicker: false,
        format: 'Y-m',
        scrollInput: false,
        validateOnBlur: false
    });
    // 风险评价间隔周期
    var $cycle = $('#JQ_cycle').select3({
        search: false,
        allowClear: false,
        data: [
            {id: '1', text: '一月'},
            {id: '2', text: '二月'},
            {id: '3', text: '三月'},
            {id: '4', text: '四月'},
            {id: '5', text: '五月'},
            {id: '6', text: '六月'},
            {id: '7', text: '七月'},
            {id: '8', text: '八月'},
            {id: '9', text: '九月'},
            {id: '10', text: '十月'},
            {id: '11', text: '十一月'},
            {id: '12', text: '十二月'}
        ]
    });
    // 提前通知评价天数
    var $advance = $('#JQ_advance');
    // 缺陷百分比
    var $percent = $('#JQ_percent');
    /**
     * 评价周期控件初始化 - end
     */


    /**
     * 插件化页面复选下拉框 - start
     */
    var $list_box_01;
    var $list_box_02;
    var $list_box_03;
    var $list_box_04;
    var $list_box_05;
    var $list_box_06;
    var $list_box_07;
    var $list_box_08;
    var $list_box_09;
    var $list_box_10;
    var $list_box_11;
    var $list_box_12;
    /**
     * 插件化页面复选下拉框 - end
     */

    // 初始行
    var $org = $('#JQ_org_line');
    // 初始添加按钮
    var $create = $('#JQ_create_cycle');
    // 多行容器
    var $mulit = $('#JQ_mulit_line');
    // 过保护
    var $too_protect = $('#JQ_too_protect');
    // 欠保护
    var $loss_protect = $('#JQ_loss_protect');
    // 交流干扰
    var $ac_disturb = $('#JQ_ac_disturb');
    // 直流干扰
    var $dc_disturb = $('#JQ_dc_disturb');
    // 共同干扰
    var $acdc_disturb = $('#JQ_acdc_disturb');

    var select3_obj1 = [
        [],
        [],
        [],
        []
    ];

    /**
     *  初始化页面数据 - start
     */
    function initPage() {
        $risk_grde_01.fit(baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].riskGrade);
        $area_level_01.fit(baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].areaLevel);
        $list_box_01 = $('#JQ_checkbox_list_01').DropCheckbox({
            list: role_list,
            data: baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].roleId,
            roleMap: role_map
        });

        $risk_grde_02.fit(baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].riskGrade);
        $area_level_02.fit(baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].areaLevel);
        $list_box_02 = $('#JQ_checkbox_list_02').DropCheckbox({
            list: role_list,
            data: baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].roleId,
            roleMap: role_map
        });

        $risk_grde_03.fit(baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].riskGrade);
        $area_level_03.fit(baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].areaLevel);
        $list_box_03 = $('#JQ_checkbox_list_03').DropCheckbox({
            list: role_list,
            data: baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].roleId,
            roleMap: role_map
        });
        /*---------------------------------------------------------------------------------*/

        var pipe = baseData.RISK_PIPE_SCHEDULE.thresholdContentJson;
        var lenth = pipe.length;
        if (lenth > 0) {
            $pipeline.fit(pipe[0].pipeLineId);
            $pipeline.element.trigger('select',[$pipeline.val(),'edit']);
            $begin.asset([{id: pipe[0].beginMileageId, text: pipe[0].beginName, mile: pipe[0].beginMileage}]);
            $end.asset([{id: pipe[0].endMileageId, text: pipe[0].endName, mile: pipe[0].endMileage}]);
            $date.val(pipe[0].firstTime);
            $cycle.fit(pipe[0].cycle);
            $advance.val(pipe[0].advance);
            $list_box_04 = $('#JQ_checkbox_list_04').DropCheckbox({
                list: role_list,
                data: pipe[0].roleId,
                roleMap: role_map
            });
            if (lenth > 1) {
                for (var i = 1; i < lenth; i++) {
                    // 新增行
                    addLine(i);
                    var $figure = $mulit.find('figure').eq(i - 1);
                    if ($figure.length) {
                        var $pipe_c = $figure.find('blockquote.org-pipe').select3({
                            allowClear: false,
                            search: false
                        }).fit(pipe[i].pipeLineId);
                        select3_obj1[0].push($pipe_c);

                        var $segment_c = $figure.find('blockquote.org-segment').select3({allowClear: false, search: false});
                        select3_obj1[1].push($segment_c);

                        var $begin_c = $figure.find('blockquote.org-begin').select3({allowClear: false, search: false}).asset([{
                            id: pipe[i].beginMileageId,
                            text: pipe[i].beginName,
                            mile: pipe[i].beginMileage
                        }]);
                        select3_obj1[2].push($begin_c);

                        var $end_c = $figure.find('blockquote.org-end').select3({allowClear: false, search: false}).asset([{
                            id: pipe[i].endMileageId,
                            text: pipe[i].endName,
                            mile: pipe[i].endMileage
                        }]);
                        select3_obj1[3].push($end_c);

                        // 触发管道
                        select3_obj1[0][i-1].element.on('select', function (e, v, idx) {
                            var id = v || select3_obj1[0][idx].val();
                            if (id) {
                                // 管段
                                DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                                    var json = DyyBase.getSelect(dataset);
                                    select3_obj1[1][idx].add(json);
                                });
                                // 起点 & 终点
                                DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                                    var begin = DyyBase.getPos(dataset, 'begin');
                                    var end = DyyBase.getPos(dataset, 'end');
                                    select3_obj1[2][idx].asset(begin);
                                    select3_obj1[3][idx].asset(end);
                                });
                            } else {
                                select3_obj1[1][idx].add([]);
                                select3_obj1[2][idx].add([]);
                                select3_obj1[3][idx].add([]);
                            }
                        });
                        // 触发管段
                        $segment_c.element.on('select', function (e, v, idx) {
                            var id = v || select3_obj1[1][idx].val();
                            if (id) {
                                // 起点 & 终点
                                DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                                    var begin = DyyBase.getPos(dataset, 'begin');
                                    var end = DyyBase.getPos(dataset, 'end');
                                    select3_obj1[2][idx].asset(begin);
                                    select3_obj1[3][idx].asset(end);
                                });
                            } else {
                                select3_obj1[2][idx].add([]);
                                select3_obj1[3][idx].add([]);
                            }
                        });

                        $figure.find('input.zy-i-input-date-0').val(pipe[i].firstTime);

                        $figure.find('blockquote.org-cycle').select3({
                            allowClear: false,
                            search: false
                        }).fit(pipe[i].cycle);

                        $figure.find('input.org-advance').val(pipe[i].advance);

                        $figure.find('div.org-check').DropCheckbox({
                            data: pipe[i].roleId,
                            roleMap: role_map
                        });
                    }
                }
            }
        }
        /*---------------------------------------------------------------------------------*/

        $list_box_05 = $('#JQ_checkbox_list_05').DropCheckbox({
            list: role_list,
            data: baseData.RISK_IMMEDIATE_REPAIR.roleId,
            roleMap: role_map
        });
        /*---------------------------------------------------------------------------------*/

        $list_box_06 = $('#JQ_checkbox_list_06').DropCheckbox({
            list: role_list,
            data: baseData.RISK_CORROSION_PERCENT.roleId,
            roleMap: role_map
        });
        $percent.val(baseData.RISK_CORROSION_PERCENT.thresholdContentJson.percent);
        /*---------------------------------------------------------------------------------*/

        $list_box_07 = $('#JQ_checkbox_list_07').DropCheckbox({
            list: role_list,
            data: baseData.RISK_MENACE_MISOPERATION.roleId,
            roleMap: role_map
        });
        /*---------------------------------------------------------------------------------*/

        $list_box_08 = $('#JQ_checkbox_list_08').DropCheckbox({
            list: role_list,
            data: baseData.RISK_MENACE_MANUFACTURE_CONSTRUCTION.roleId,
            roleMap: role_map
        });
        /*---------------------------------------------------------------------------------*/

        $list_box_09 = $('#JQ_checkbox_list_09').DropCheckbox({
            list: role_list,
            data: baseData.RISK_MENACE_GEOLOGIC_HAZARD.roleId,
            roleMap: role_map
        });
        /*---------------------------------------------------------------------------------*/

        $list_box_10 = $('#JQ_checkbox_list_10').DropCheckbox({
            list: role_list,
            data: baseData.RISK_MENACE_THIRD_PARTY_DAMAGE.roleId,
            roleMap: role_map
        });
        /*---------------------------------------------------------------------------------*/

        $list_box_11 = $('#JQ_checkbox_list_11').DropCheckbox({
            list: role_list,
            data: baseData.RISK_OVER_UNDER_PROTECTION.roleId,
            roleMap: role_map
        });
        if(baseData.RISK_OVER_UNDER_PROTECTION.thresholdContentJson.overProtection){
            $too_protect.addClass('active');
        }
        if(baseData.RISK_OVER_UNDER_PROTECTION.thresholdContentJson.underProtection){
            $loss_protect.addClass('active');
        }
        /*---------------------------------------------------------------------------------*/

        $list_box_12 = $('#JQ_checkbox_list_12').DropCheckbox({
            list: role_list,
            data: baseData.RISK_AC_DC_INTERFERENCE.roleId,
            roleMap: role_map
        });
        if(baseData.RISK_AC_DC_INTERFERENCE.thresholdContentJson.acInterference){
            $ac_disturb.addClass('active');
        }
        if(baseData.RISK_AC_DC_INTERFERENCE.thresholdContentJson.dcInterference){
            $dc_disturb.addClass('active');
        }
        if(baseData.RISK_AC_DC_INTERFERENCE.thresholdContentJson.acDcInterference){
            $acdc_disturb.addClass('active');
        }
        /*---------------------------------------------------------------------------------*/
    }

    initPage();
    /**
     *  初始化页面数据 - end
     */


    /**
     * 添加行操作 - start
     */
        // 初始添加行按钮
    $create.click(function () {
        addLine();
    });

    // 添加行操作
    function addLine(idx) {

        // 建立索引
        var t_idx = 0;
        if(idx != undefined){
            t_idx = --idx;
        }

        var $copy = $org.find('figure').clone(true);

        // 管道
        var $pipe = $copy.find('blockquote.org-pipe').select3({
            index: t_idx,
            data: $pipeline.val('store'),
            allowClear: false
        });
        // 管段
        var $segment = $copy.find('blockquote.org-segment').select3({
            index: t_idx,
            title: '管段',
            search: false,
            allowClear: false
        });
        // 起点位置
        var $begin = $copy.find('blockquote.org-begin').select3({
            index: t_idx,
            title: '起点位置',
            server: true,
            allowClear: false,
            param: {
                'pipeLineId': $pipeline,
                'pipeSegmentId': $segment
            }
        });
        // 终点位置
        var $end = $copy.find('blockquote.org-end').select3({
            index: t_idx,
            title: '终点位置',
            server: true,
            allowClear: false,
            param: {
                'pipeLineId': $pipeline,
                'pipeSegmentId': $segment,
                'beginSignId': $begin
            }
        });
        // 触发管道
        $pipe.element.on('select', function (e, v) {
            var id = v || $pipe.val();
            if (id) {
                // 管段
                DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                    var json = DyyBase.getSelect(dataset);
                    $segment.add(json);
                });
                // 起点 & 终点
                DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                    var begin = DyyBase.getPos(dataset, 'begin');
                    var end = DyyBase.getPos(dataset, 'end');
                    $begin.asset(begin);
                    $end.asset(end);
                });
            } else {
                $segment.add([]);
                $begin.add([]);
                $end.add([]);
            }
        });
        // 触发管段
        $segment.element.on('select', function (e, v) {
            var id = v || $segment.val();
            if (id) {
                // 起点 & 终点
                DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                    var begin = DyyBase.getPos(dataset, 'begin');
                    var end = DyyBase.getPos(dataset, 'end');
                    $begin.asset(begin);
                    $end.asset(end);
                });
            } else {
                $begin.add([]);
                $end.add([]);
            }
        });

        // 周期
        var $cycle = $copy.find('blockquote.org-cycle').select3({
            search: false,
            allowClear: false,
            data: [
                {id: '1', text: '一月'},
                {id: '2', text: '二月'},
                {id: '3', text: '三月'},
                {id: '4', text: '四月'},
                {id: '5', text: '五月'},
                {id: '6', text: '六月'},
                {id: '7', text: '七月'},
                {id: '8', text: '八月'},
                {id: '9', text: '九月'},
                {id: '10', text: '十月'},
                {id: '11', text: '十一月'},
                {id: '12', text: '十二月'}
            ]
        });
        // 角色
        var $role = $copy.find('div.org-check').DropCheckbox({
            list: role_list
        });
        // 初次评价时间
        var $advance = $copy.find('input.zy-i-input-date-0');
        $advance.datetimepicker({
            timepicker: false,
            format: 'Y-m',
            scrollInput: false,
            validateOnBlur: false
        });


        // 删除按钮
        var $addbtn = $copy.find('button.zy-delete-s');
        $addbtn.off('click').click(function () {
            $(this).parent('div').parent('div').parent('figure').remove();
        });
        // 添加按钮
        var $deletebtn = $copy.find('button.zy-create-s');
        $deletebtn.off('click').click(function () {
            addLine();
        });

        // 追加整行
        $mulit.append($copy);
    }

    /**
     * 添加行操作 - end
     */


    /**
     * 注入数据 - start
     */
    function pushData() {
        // 系统风险等级
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].areaLevel = $area_level_01.val();
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].riskGrade = $risk_grde_01.val();
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].roleId = $list_box_01.spans.join(',');
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[0].order = 1;

        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].areaLevel = $area_level_02.val();
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].riskGrade = $risk_grde_02.val();
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].roleId = $list_box_02.spans.join(',');
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[1].order = 2;

        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].areaLevel = $area_level_03.val();
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].riskGrade = $risk_grde_03.val();
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].roleId = $list_box_03.spans.join(',');
        baseData.RISK_SYSTEM_SCHEDULE.thresholdContentJson[2].order = 3;

        baseData.RISK_PIPE_SCHEDULE.thresholdContentJson = [];

        // 管道周期设置
        var pipe = {};
        pipe.advance = $advance.val();
        pipe.cycle = $cycle.val();
        pipe.firstTime = $date.val();
        pipe.pipeLineId = $pipeline.val();
        pipe.pipeLineName = $pipeline.val('text');
        pipe.beginMileageId = $begin.val();
        pipe.beginName = $begin.val('text');
        pipe.endMileageId = $end.val();
        pipe.endName = $end.val('text');
        pipe.roleId = $list_box_04.spans.join(',');
        pipe.order = 1;
        baseData.RISK_PIPE_SCHEDULE.thresholdContentJson.push(pipe);

        var $figures = $mulit.find('figure'), order = 1;
        if ($figures.length > 0) {
            $figures.each(function (i) {
                var $this = $(this);
                var pipes = {};
                pipes.advance = $this.find('input.org-advance').val();
                pipes.cycle = $this.find('blockquote.org-cycle').data('value');
                pipes.firstTime = $this.find('input.zy-i-input-date-0').val();
                pipes.pipeLineId = $this.find('blockquote.org-pipe').data('value');
                pipes.pipeLineName = $this.find('blockquote.org-pipe').data('text');
                pipes.beginMileageId = $this.find('blockquote.org-begin').data('value');
                pipes.beginName = $this.find('blockquote.org-begin').data('text');
                pipes.endMileageId = $this.find('blockquote.org-end').data('value');
                pipes.endName = $this.find('blockquote.org-end').data('text');
                pipes.roleId = $this.find('div.org-check').data('store');
                pipes.order = ++i + order;
                if (pipes.pipeLineId) {
                    baseData.RISK_PIPE_SCHEDULE.thresholdContentJson.push(pipes);
                }
            });
        }

        // 立即修复的管体缺陷 - 若管段存在需要立即修复的管体缺陷，提醒开展缺陷修复工作
        baseData.RISK_IMMEDIATE_REPAIR.roleId = $list_box_05.spans.join(',');

        // 内腐蚀占比
        baseData.RISK_CORROSION_PERCENT.roleId = $list_box_06.spans.join(',');
        baseData.RISK_CORROSION_PERCENT.thresholdContentJson.percent = $percent.val();

        // 过保护 - 欠保护
        baseData.RISK_OVER_UNDER_PROTECTION.roleId = $list_box_11.spans.join(',');
        baseData.RISK_OVER_UNDER_PROTECTION.thresholdContentJson.overProtection = $too_protect.hasClass('active') ? 1 : '';
        baseData.RISK_OVER_UNDER_PROTECTION.thresholdContentJson.underProtection = $loss_protect.hasClass('active') ? 1 : '';

        // 交流 - 直流 - 共同干扰
        baseData.RISK_AC_DC_INTERFERENCE.roleId = $list_box_12.spans.join(',');
        baseData.RISK_AC_DC_INTERFERENCE.thresholdContentJson.acInterference = $ac_disturb.hasClass('active') ? 1 : '';
        baseData.RISK_AC_DC_INTERFERENCE.thresholdContentJson.dcInterference = $dc_disturb.hasClass('active') ? 1 : '';
        baseData.RISK_AC_DC_INTERFERENCE.thresholdContentJson.acDcInterference = $acdc_disturb.hasClass('active') ? 1 : '';

        // 误操作
        baseData.RISK_MENACE_MISOPERATION.roleId = $list_box_07.spans.join(',');

        // 制造和施工
        baseData.RISK_MENACE_MANUFACTURE_CONSTRUCTION.roleId = $list_box_08.spans.join(',');

        // 地质灾害
        baseData.RISK_MENACE_GEOLOGIC_HAZARD.roleId = $list_box_09.spans.join(',');

        // 第三方破坏
        baseData.RISK_MENACE_THIRD_PARTY_DAMAGE.roleId = $list_box_10.spans.join(',');

        return baseData;
    }

    /**
     * 注入数据 - end
     */


    /**
     * 校验数据重复性有效性 - start
     */
    function checkData() {

        var mode = true;

        // 风险推送
        // 等级
        var grade_01 = $risk_grde_01.val(), grade_02 = $risk_grde_02.val(), grade_03 = $risk_grde_03.val();
        // 高后果区
        var area_01 = $area_level_01.val(), area_02 = $area_level_02.val(), area_03 = $area_level_03.val();
        if (grade_01 == grade_02) {
            $(this).MSG({
                type: 'error',
                content: '与已有风险推送设置重复，请重新设置！'
            });
            return false;
        }
        if (grade_01 == grade_03) {
            $(this).MSG({
                type: 'error',
                content: '与已有风险推送设置重复，请重新设置！'
            });
            return false;
        }
        if (grade_02 == grade_03) {
            $(this).MSG({
                type: 'error',
                content: '与已有风险推送设置重复，请重新设置！'
            });
            return false;
        }

        // 多行 -> 管道 - 起点 - 终点
        var trunk = [[], [], []];
        trunk[0].push($pipeline.val());
        trunk[1].push($begin.val());
        trunk[2].push($end.val());

        var $figures = $mulit.find('figure');
        if ($figures.length > 0) {
            $figures.each(function () {
                var $this = $(this);
                var $pipe = $this.find('blockquote.org-pipe');
                var $begin = $this.find('blockquote.org-begin');
                var $end = $this.find('blockquote.org-end');

                var v_pipe = $pipe.data('value');
                var v_begin = $begin.data('value');
                var v_end = $end.data('value');

                if (v_pipe) {
                    trunk[0].push(v_pipe);
                }
                if (v_begin) {
                    trunk[1].push(v_begin);
                }
                if (v_end) {
                    trunk[2].push(v_end);
                }
            });

            if (trunk[0].length !== trunk[1].length) {
                $(this).MSG({
                    type: 'error',
                    content: '管道起点位置不完整!'
                });
                return false;
            }
            if (trunk[0].length !== trunk[2].length) {
                $(this).MSG({
                    type: 'error',
                    content: '管道终点位置不完整!'
                });
                return false;
            }

            var temp_pipe = trunk[0].join(',') + ',';
            var temp_begin = trunk[1].join(',') + ',';
            var temp_end = trunk[2].join(',') + ',';
            for (var i = 0, len = trunk[0].length; i < len; i++) {
                if (temp_pipe.replace((trunk[0][i] + ','), '').indexOf((trunk[0][i] + ',')) > -1) {
                    if(temp_begin.replace((trunk[1][i] + ','), '').indexOf((trunk[1][i] + ',')) > -1){
                        if(temp_end.replace((trunk[2][i] + ','), '').indexOf((trunk[2][i] + ',')) > -1){
                            ++i;
                            $(this).MSG({
                                type: 'error',
                                content: '第' + i + '条管道存在重复设置!'
                            });
                            return false;
                        }
                    }
                }
            }

            // 时间 - 天数
            var $dates = $figures.find('input.zy-i-input-date-0'), $days = $figures.find('input.org-advance');
            if($dates.length > 0){
                $dates.each(function(){
                    var $this = $(this);
                    var v = $.trim($this.val());
                    if(v){
                        var temp_v = v.split('-');
                        if(temp_v.length === 2){
                            if(isNaN(temp_v[0]) || isNaN(temp_v[1])){
                                $this.TIP({content: '初次评价时间格式不正确!'});
                                mode = false;
                            }else{
                                if(temp_v[1] < 1 || temp_v[1] >= 13){
                                    $this.TIP({content: '初次评价时间格式不正确!'});
                                    mode = false;
                                }
                            }
                        }else{
                            $this.TIP({content: '初次评价时间格式不正确!'});
                            mode = false;
                        }
                    }else{
                        $this.TIP({content: '初次评价时间不能为空!'});
                        mode = false;
                    }
                });
            }
            if($days.length > 0){
                $days.each(function(){
                    var $this = $(this);
                    var v = $.trim($this.val());
                    if(v){
                        if(!(/^[1-9]\d*$/g.test(v))){
                            $this.TIP({content: '提前通知评价天数须为正整数!'});
                            mode = false;
                        }
                    }else{
                        $this.TIP({content: '提前通知评价天数不能为空!'});
                        mode = false;
                    }
                });
            }
        }

        // 单行 -> 时间 - 天数
        var date = $.trim($date.val()), day = $.trim($advance.val());
        if(date){
            var temp_v = date.split('-');
            if(temp_v.length === 2){
                if(isNaN(temp_v[0]) || isNaN(temp_v[1])){
                    $date.TIP({content: '初次评价时间格式不正确!'});
                    mode = false;
                }else{
                    if(temp_v[1] < 1 || temp_v[1] >= 13){
                        $date.TIP({content: '初次评价时间格式不正确!'});
                        mode = false;
                    }
                }
            }else{
                $date.TIP({content: '初次评价时间格式不正确!'});
                mode = false;
            }
        }else{
            $date.TIP({content: '初次评价时间不能为空!'});
            mode = false;
        }

        if(day){
            if(!(/^[1-9]\d*$/g.test(day))){
                $advance.TIP({content: '提前通知评价天数须为正整数!'});
                mode = false;
            }
        }else{
            $advance.TIP({content: '提前通知评价天数不能为空!'});
            mode = false;
        }

        // 百分比
        var percent = $.trim($percent.val());
        if(percent){
            if(isNaN(percent)){
                $percent.TIP({content: '缺陷百分比须为数字!'});
                mode = false;
            }else{
                if(percent > 100 || percent < 0){
                    $percent.TIP({content: '缺陷百分比须范围0~100!'});
                    mode = false;
                }else{
                    var temp_per = percent.split('.');
                    if(temp_per.length === 2){
                        if(/^00/g.test(temp_per[0])){
                            $percent.TIP({content: '缺陷百分比须为数字!'});
                            mode = false;
                        }
                    }
                }
            }
        }else{
            $percent.TIP({content: '缺陷百分比不能为空!'});
            mode = false;
        }

        return mode;
    }

    /**
     * 校验数据重复性有效性 - end
     */


    // 保存更改设置
    $('#JQ_save_settings').click(function () {
        if(checkData()){
            var $this = $(this);
            if ($this.attr('data-status') === 'true') {
                $this.attr('data-status', 'false');
                var data = JSON.stringify(pushData());
                DyyBase.ajax(sysApi.url_settings_risk_save(), data, function (dataset) {
                    $this.attr('data-status', 'true');
                    if (dataset.bool) {
                        $this.MSG({
                            type: 'success',
                            content: '风险评价设置成功！'
                        });
                    } else {
                        $this.MSG({
                            type: 'error',
                            content: '风险评价设置失败！'
                        });
                    }
                }, {headers : {"Content-Type" : "application/json"}});
            }
        }
    });

    // 恢复默认设置
    $('#JQ_save_defaults').click(function () {
        if(checkData()){
            var $this = $(this);
            if ($this.attr('data-status') === 'true') {
                $this.attr('data-status', 'false');
                DyyBase.ajax(sysApi.url_settings_risk_cancel(), {}, function (dataset) {
                    $this.attr('data-status', 'true');
                    if (dataset.bool) {
                        $(this).MSG({
                            type: 'success',
                            content: '风险评价恢复默认设置成功！',
                            closeBtnExist: false,
                            yesBtnAct: DyyBase.pageReload
                        });
                    } else {
                        $(this).MSG({
                            type: 'error',
                            closeBtnExist: false,
                            content: '风险评价恢复默认设置失败！'
                        });
                    }
                });
            }
        }
    });

})(jQuery, window, document);
